package cn.jinhe.doc.view.action;

import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.jinhe.doc.base.BaseAction;
import cn.jinhe.doc.domain.FixedAssts;
import cn.jinhe.doc.domain.Material;
import cn.jinhe.doc.domain.MaterialType;
import cn.jinhe.doc.domain.User;
import cn.jinhe.doc.domain.UserReceipt;
import cn.jinhe.doc.util.QueryHelper;

@Controller
@Scope("prototype")
public class UserReceiptAction extends BaseAction<UserReceipt>{
	
	private String searchSerialNo;

	private Long fixedAsstsId;
	private Long userId;
	private Long processerId;
	private int delivery;
	public int getDelivery() {
		return delivery;
	}
	public void setDelivery(int delivery) {
		this.delivery = delivery;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	private Long memoryId;
	
	private Long moniterId;
	private Long cdRomId;
	private Long graphicCardId;
	private Long hardDiskId;
	

	public Long getFixedAsstsId() {
		return fixedAsstsId;
	}
	public void setFixedAsstsId(Long fixedAsstsId) {
		this.fixedAsstsId = fixedAsstsId;
	}
	public String show() throws Exception {
		// 准备数据:topic
		UserReceipt userReceipt = userReceiptService.getById(model.getId());
		ActionContext.getContext().put("userReceipt", userReceipt);
		
/*		List<Step> stepList=stepService.findByuserReceipt(userReceipt);
		ActionContext.getContext().put("stepList", stepList);*/
		
		return "show";

		
	}
	public String list() throws Exception {
		new QueryHelper(UserReceipt.class, "p").preparePageBean(userReceiptService, pageNum,
				pageSize);

		return "list";
	}

	/*
	 * 删除
	 */
	public String delete() throws Exception {
		userReceiptService.delete(model.getId());
		return "toList";
	}

	/*
	 * 添加
	 */
	public String add() throws Exception {	
		model.setUser(userService.getById(userId));
		model.setFixedAssts(fixedAsstsService.getById(fixedAsstsId));
		model.setDelivery(delivery);
		model.setProcesser(materialService.getById(processerId));
		model.setMemory(materialService.getById(memoryId));
		model.setHardDisk(materialService.getById(hardDiskId));
		model.setgCard(materialService.getById(graphicCardId));
		model.setMoniter(materialService.getById(moniterId));
		model.setCdRom(materialService.getById(cdRomId));

		model.setTitle(model.getUser().getName()+"  用户电脑签收表  "+new Date());
		model.setRecorder(getCurrentUser());
		model.setPostTime(new Date());
		userReceiptService.save(model);
		return "toList";
	}

	public String setFixedAssts() throws Exception{
	    MaterialType searchMa=	materialTypeService.getById((long) 1);
		
		if(searchSerialNo==null){
			new QueryHelper(FixedAssts.class, "f")
			.addCondition("f.material.materialType =?", searchMa)
			//.addCondition("f.material.materialType.id=?",1)
			//.addCondition("f.serialNo like :?", searchSerialNo)
			.preparePageBean(fixedAsstsService, pageNum,
					pageSize);
			
		}
		
		if(searchSerialNo!=null){
		new QueryHelper(FixedAssts.class, "f")
		//.addCondition("f.material.materialType.id=?",1)
//		.addCondition("f.serialNo like :?", searchSerialNo)
		.addCondition("f.fixAsstsNo like '%"+searchSerialNo+"%'")
	    .addCondition("f.material.materialType =?", searchMa)
		.preparePageBean(fixedAsstsService, pageNum,
				pageSize);}
//	    List<FixedAssts> fixedAsstsList	=fixedAsstsService.findSearch(searchSerialNo);
//	    ActionContext.getContext().put("fixedAsstsList", fixedAsstsList);
		
		return "setFixedAssts";
	}
	
	/*
	 * 添加页面
	 */
	public String addUI() throws Exception {
		
//		List<User> userList=userService.findAll();
//		ActionContext.getContext().put("userList", userList);
		
		List<User> userList=userService.findAll();
		ActionContext.getContext().put("userList", userList);
		
		/* 显卡信息集合 */
		MaterialType materialTypeGCard=materialTypeService.getById((long)2);
		List<Material> gCardList=materialService.getByMaterialTypeId(materialTypeGCard);
		ActionContext.getContext().put("gCardList",gCardList);
		
		/* 处理器信息集合 */
		MaterialType materialTypeProcesser=materialTypeService.getById((long)3);
		List<Material> processerList=materialService.getByMaterialTypeId(materialTypeProcesser);
		ActionContext.getContext().put("processerList",processerList);
		
		/* 内存信息集合 */
		MaterialType materialTypeMemory=materialTypeService.getById((long)4);
		List<Material> memoryList=materialService.getByMaterialTypeId(materialTypeMemory);
		ActionContext.getContext().put("memoryList",memoryList);
		
		
		/* 硬盘信息集合 */
		MaterialType materialTypeHardDisk=materialTypeService.getById((long)5);
		List<Material> hardDiskList=materialService.getByMaterialTypeId(materialTypeHardDisk);
		ActionContext.getContext().put("hardDiskList",hardDiskList);
		
		
		/* 显示器信息集合 */
		MaterialType materialTypeMonitor=materialTypeService.getById((long)6);
		List<Material> monitorList=materialService.getByMaterialTypeId(materialTypeMonitor);
		ActionContext.getContext().put("monitorList",monitorList);
		
		
		/* 光驱信息集合 */
		MaterialType materialTypeCdRom=materialTypeService.getById((long)7);
		List<Material> cdRomList=materialService.getByMaterialTypeId(materialTypeCdRom);
		ActionContext.getContext().put("cdRomList",cdRomList);
		
		
/*		List<FixedAssts> fixedAsstsList=fixedAsstsService.findSnAndFn();
		ActionContext.getContext().put("fixedAsstsList", fixedAsstsList);*/
		return "saveUI";
	}

	/*
	 * 修改页面
	 */
	public String editUI() throws Exception {
		List<User> userList=userService.findAll();
		ActionContext.getContext().put("userList", userList);
		

		
		/* 处理器信息集合 */
		MaterialType materialTypeProcesser=materialTypeService.getById((long)3);
		List<Material> processerList=materialService.getByMaterialTypeId(materialTypeProcesser);
		ActionContext.getContext().put("processerList",processerList);
		
		/* 内存信息集合 */
		MaterialType materialTypeMemory=materialTypeService.getById((long)4);
		List<Material> memoryList=materialService.getByMaterialTypeId(materialTypeMemory);
		ActionContext.getContext().put("memoryList",memoryList);
		
		
		/* 硬盘信息集合 */
		MaterialType materialTypeHardDisk=materialTypeService.getById((long)5);
		List<Material> hardDiskList=materialService.getByMaterialTypeId(materialTypeHardDisk);
		ActionContext.getContext().put("hardDiskList",hardDiskList);
		
		
		/* 显示器信息集合 */
		MaterialType materialTypeMonitor=materialTypeService.getById((long)6);
		List<Material> monitorList=materialService.getByMaterialTypeId(materialTypeMonitor);
		ActionContext.getContext().put("monitorList",monitorList);
		
		
		/* 光驱信息集合 */
		MaterialType materialTypeCdRom=materialTypeService.getById((long)7);
		List<Material> cdRomList=materialService.getByMaterialTypeId(materialTypeCdRom);
		ActionContext.getContext().put("cdRomList",cdRomList);
		
		/* 显卡信息集合 */
		MaterialType materialTypeGCard=materialTypeService.getById((long)2);
		List<Material> gCardList=materialService.getByMaterialTypeId(materialTypeGCard);
		ActionContext.getContext().put("gCardList",gCardList);
		
		
		UserReceipt userReceipt=userReceiptService.getById(model.getId());
		fixedAsstsId=userReceipt.getFixedAssts().getId();
		ActionContext.getContext().getValueStack().push(userReceipt);
		return "saveUI";
	}

	/*
	 * 修改
	 */
	public String edit() throws Exception {
		UserReceipt userReceipt=userReceiptService.getById(model.getId());

//		userReceipt.setTitle(model.getTitle());
/*		userReceipt.setContent(model.getContent());
*/		
		userReceiptService.update(userReceipt);
		return "toList";
	}
	
	
	public String getSearchSerialNo() {
		return searchSerialNo;
	}
	public void setSearchSerialNo(String searchSerialNo) {
		this.searchSerialNo = searchSerialNo;
	}
	public Long getProcesserId() {
		return processerId;
	}
	public void setProcesserId(Long processerId) {
		this.processerId = processerId;
	}
	public Long getMemoryId() {
		return memoryId;
	}
	public void setMemoryId(Long memoryId) {
		this.memoryId = memoryId;
	}
	public Long getMoniterId() {
		return moniterId;
	}
	public void setMoniterId(Long moniterId) {
		this.moniterId = moniterId;
	}
	public Long getCdRomId() {
		return cdRomId;
	}
	public void setCdRomId(Long cdRomId) {
		this.cdRomId = cdRomId;
	}

	public Long getGraphicCardId() {
		return graphicCardId;
	}
	public void setGraphicCardId(Long graphicCardId) {
		this.graphicCardId = graphicCardId;
	}
	public Long getHardDiskId() {
		return hardDiskId;
	}
	public void setHardDiskId(Long hardDiskId) {
		this.hardDiskId = hardDiskId;
	}
	
}
